//
//  DSUnitInnerView.m
//  DSJY
//
//  Created by 冯华强 on 16/9/25.
//  Copyright © 2016年 dsjy. All rights reserved.
//  练习的View

#import "DSUnitInnerView.h"
#import "Masonry.h"
#import "DSUnitListeningContent.h"
#import "UIImageView+WebCache.h"
#import "MTConst.h"
#import "XCYAudioTool.h"
#import "UIColor+Extension.h"

@interface DSUnitInnerView ()
//背景
@property (nonatomic, weak, readwrite) UIImageView *bgView;
//图片
@property (nonatomic, weak, readwrite) UIImageView *iconView;
//提示对错的图标
@property (nonatomic, weak, readwrite) UIImageView *msgView;
//key：bool
@property (nonatomic, strong) NSDictionary<NSNumber *, NSString *> *msgImageDict;
@end

@implementation DSUnitInnerView

#pragma mark - 懒加载
- (NSDictionary<NSNumber *,NSString *> *)msgImageDict
{
    if (_msgImageDict == nil) {
        _msgImageDict = @{@(NO) : @"wrong", @(YES) : @"right"};
    }
    return _msgImageDict;
}

#pragma mark - 系统回调
//初始化
+ (__kindof DSUnitInnerView *)unitInnerView
{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self setupView];
    }
    return self;
}

#pragma mark - 设置UI
- (void)setupView
{
    //图片
//    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgs"]];
//    bgView.layer.cornerRadius = 10;
//    bgView.clipsToBounds = YES;
//    [self addSubview:bgView];
//    self.bgView = bgView;
    
    //图片
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.contentMode = UIViewContentModeScaleAspectFit;
    iconView.layer.cornerRadius = 8;
    iconView.clipsToBounds = YES;
    iconView.userInteractionEnabled = YES;
    UITapGestureRecognizer *gestTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconViewClick)];
    [iconView addGestureRecognizer:gestTap];
    [self addSubview:iconView];
    self.iconView = iconView;
    
    //提示对错的图标
    UIImageView *msgView = [[UIImageView alloc] init];
    msgView.contentMode = UIViewContentModeScaleAspectFit;
    msgView.alpha = 0.0;  //一开始隐藏
    [self addSubview:msgView];
    self.msgView = msgView;
    
    
//    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(5);
//        make.right.mas_equalTo(-5);
//        make.bottom.mas_equalTo(-5);
//        make.top.mas_equalTo(5);
//    }];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(-15);
        make.top.mas_equalTo(15);
    }];
    
    [msgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(80);
        make.center.equalTo(iconView);
    }];
}

- (void)setListeningContent:(DSUnitListeningContent *)listeningContent
{
    _listeningContent = listeningContent;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:listeningContent.srcString] placeholderImage:[UIImage imageNamed:@"defaultH310"]];
}

- (void)setRight:(BOOL)right
{
    _right = right;
    
    //设置正确/错误提示图片
    NSString *name = self.msgImageDict[@(right)];
    self.msgView.image = [UIImage imageNamed:name];
    //提示音乐
    [XCYAudioTool playAudioWithFileName:[NSString stringWithFormat:@"%@.mp3", name]];
    
    //执行动画－隐藏－显示－隐藏
    self.msgView.alpha = 0.0;
    [UIView animateWithDuration:0.5 animations:^{
        self.msgView.alpha = 1.0;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.msgView.alpha = 0.0;
        }];
    }];
}

#pragma mark - 设置事件
- (void)iconViewClick
{
    [MTNotificationCenter postNotificationName:DSUnitInnerViewDidIconViewClickNote object:self];
}

@end
